package com.client;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;

import com.mongodb.MongoException;
import com.storage.mongo.DBInterface;
import com.wink.charity.Charity;
import com.wink.charity.CharityList;
import com.wink.common.Login;
import com.wink.common.Publish;
import com.wink.dt.*;
import com.wink.event.*;
import com.wink.subscribe.Subscription;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;


public class ReachOutClient {
	private String endPoint = "http://localhost:8080/ReachOut/rest/community";

	public ReachOutClient() {
	}

	public ReachOutClient(String url) {
		endPoint = url;
	}
	
	public void demoServices() {
		System.out.println("\n **List demoServices: ");
		try {
			String url = endPoint + "/services";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			// we can get the return as a string or converted to an object
			// String response =
			// resource.accept(MediaType.APPLICATION_XML).get(String.class);
			@SuppressWarnings("unchecked")
			//Event e = resource.accept(MediaType.APPLICATION_XML).get(Event.class);
			String services = resource.accept(MediaType.TEXT_PLAIN).get(String.class);
			System.out.println("Services :" + services);			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demolistCharities() {		
		System.out.println("\n **Client --> In listCharities");
		try {
			String url = endPoint + "/charity";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			@SuppressWarnings("unchecked")
			String list = resource.accept(MediaType.APPLICATION_JSON).get(String.class);
			//System.out.println("ListCharity :Json ::" +  list);
			/*System.out.println("Charities:");
			for (Charity s : list.getList()) {
				System.out.println("Charity : " + s.getName() + 
						" Description " +  s.getDescription() +
						" Identity " +  s.getIdentity().toString());	
				System.out.println("Charity :Json ::" +  s.toJson(s));
			}*/
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoCreateCharity() {		
		System.out.println("\n **List demoCreateCharity: ");
		try {
			String url = endPoint + "/charity";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);
			
			Charity c = new Charity();
			c.setName("CharityFinalTest1");
			c.setDescription("Test creating a Charity, Charity 11 from a client1");
			c.setAmount("66661");
			c.setCommunityID("ABPM_FinalTest1");
			c.setCreatedBy("PRK_Test1");
					
			//c.setIdentity(identity);

			@SuppressWarnings("unchecked")
			//Charity created = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).put(Charity.class, c);
			String created = resource.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(String.class, Charity.toJson(c));
					
			System.out.println("Created Charity : " + created);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void demoGetCharityByName() {
		System.out.println("\n** demoGetCharityByName:");
		try {
			Charity c = new Charity();
			c.setName("CharityFinalTest1");
			//String charityID="4f9e46a44cd4adb319c1ba61";
			//String url = endPoint + "/charity/" + charityName +"/" +charityID;
			String url = endPoint + "/charity/name";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			

			String response = resource.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(String.class,Charity.toJson(c));
			//String response = resource.accept(MediaType.APPLICATION_XML).get(String.class);

			System.out.println("Client --> demoGetCharityByName : " + response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoGetParticularCharity() {
		System.out.println("\n** demoGetParticularCharity:");
		try {
			//String charityName = "Charity5";
			String charityID="4f9e46a44cd4adb319c1ba61";
			String url = endPoint + "/charity/" + charityID;
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			

			String response = resource.accept(MediaType.APPLICATION_JSON).get(String.class);

			System.out.println("Client --> demoGetCharityByName : " + response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void donateCharity() {		
		System.out.println("\n **List donateCharity: ");
		try {
			String url = endPoint + "/charity/donate";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);	
			
			/*Charity c = new Charity();
			c.setName(" Charity from Client 11");
			c.setAmount("20");*/
			
			Charity c = new Charity();
			c.setName(" Charity from Client 11");
			c.setDescription("Test creating a Charity, Charity 11 from a client");
				//Identity identity = new Identity();
				//identity.setFromCommunity("ABPMClient");
				//identity.setFromUser("PKRClient");
//				''identity.setDate(new Date());	
			//c.setIdentity(identity);
			c.setAmount("20");

			@SuppressWarnings("unchecked")
			String donated = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).post(String.class,Charity.toJson(c));
					
			System.out.println("Donated to Charity : " + donated);				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void donateCharity1() {		
		System.out.println("\n **List donateCharity1: ");
		try {
			String charityID="4fa8da524cd43b27c8fc6d60";
			String url = endPoint + "/charity/"+ charityID;
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);	

			
			Charity c = new Charity();
			c.setCreatedBy("PRK");
			c.setCommunityID("ABPM");
			c.setAmount("1");

			@SuppressWarnings("unchecked")
			String donated = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).post(String.class, Charity.toJson(c));
					
			System.out.println("Donated to Charity : " + donated);				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void demolistDiscussion() {		
		System.out.println("\n **Client --> In demolistDiscussion");
		try {
			String url = endPoint + "/discussionboard";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			@SuppressWarnings("unchecked")
			String list = resource.accept(MediaType.APPLICATION_XML).get(String.class);
			//System.out.println("ListTopics :Json ::" +  list);
			/*System.out.println("Charities:");
			for (Charity s : list.getList()) {
				System.out.println("Charity : " + s.getName() + 
						" Description " +  s.getDescription() +
						" Identity " +  s.getIdentity().toString());	
				System.out.println("Charity :Json ::" +  s.toJson(s));
			}*/
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoCreateTopic() {		
		System.out.println("\n **List demoCreateTopic: ");
		try {
			String url = endPoint + "/discussionboard";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			
	
			Topic t = new Topic();
			t.setTopicTitle("IPL");		
			t.setTopicdescription("Petrol prices increase");
				t.setCreatedBy("PRK");
				t.setCommunityID("ABPM");
				t.setDate((new Date()).toString());
				
			
				// create Comment1 under Topic News
			Comment c = new Comment();
				c.setComment("News Commentssss");	
				c.setTopicName("News Topic");
				c.setCreatedBy("PRK");

				t.addToCommentList(c);
				
//				c = new Comment();
//				c.setComment("News Comment 2");
//				c.setTopicName("News Topic");	
//					identity2 = new Identity();
//					identity2.setFromCommunity("from Community ABPM");
//					identity2.setFromUser("from User AVP");
//					identity2.setDate(new Date());
//				c.setIdentity(identity2);
//				t.addToCommentList(c);

			@SuppressWarnings("unchecked")
			String created = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).put(String.class, Topic.toJson(t));
					
			System.out.println("Created Topic : " + created);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoCreateComment() {		
		System.out.println("\n **List demoCreateComment: ");
		try {
			String url = endPoint + "/discussionboard/comment";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			
			
			Comment c = new Comment();
				c.setComment("PW beat CSK !");	
				c.setTopicName("IPL");
				c.setCreatedBy("PRK");
				
//				Identity identity2 = new Identity();
//					identity2.setFromCommunity("from Community ABPM");
//					identity2.setFromUser("from User PRK");
//					identity2.setDate(new Date());
//				c.setIdentity(identity2);				

			@SuppressWarnings("unchecked")
			String created = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).put(String.class, Comment.toJson(c));
					
			System.out.println("Created Comment : " + created);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoGetTopicByName() {
		System.out.println("\n** demoGetTopicByName:");
		try {
			// change according to data in mongoDB
			String topicName = "IPL";
			String url = endPoint + "/discussionboard/1/" + topicName  ;
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			

			String response = resource.accept(MediaType.APPLICATION_XML).get(String.class);

			System.out.println("Client --> demoGetTopicByName : " + response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoGetTopicDetails() {
		System.out.println("\n** demoGetTopicDetails:");
		try {
			// change according to data in mongoDB
			String topicName = "IPL";
			String url = endPoint + "/discussionboard/2/" + topicName  ;
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			

			String response = resource.accept(MediaType.APPLICATION_XML).get(String.class);

			System.out.println("Client --> demoGetTopicDetails : " + response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoSubscribe() {
		System.out.println("\n** subcribeDemo:");
		try {
			String url = endPoint + "/subscribe/charity";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			
			
			Subscription sub = new Subscription();
			sub.setEmail("Shilka7@gmail.com");
			sub.setName("Charity1");
//			sub.setAmount("500");
//			sub.setCommunity("Planet Granite");
//			sub.setCommunityID("SV");
			
			resource.contentType(MediaType.APPLICATION_JSON);
			resource.post(sub);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void demoLogin() {		
		System.out.println("\n **List demoLogin: ");
		try {
			String url = endPoint + "/login";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);
			
			Login c = new Login();
			c.setName("ABPM");
			c.setPassword("password");
	
			@SuppressWarnings("unchecked")
			String communityID = resource.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(String.class, Login.toJson(c));
					
			System.out.println("Created communityID : " + communityID);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void demoPublish() {		
		System.out.println("\n **List demoPublish: ");
		try {
			String url = endPoint + "/publish";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);
			
			Publish c = new Publish();
			c.setServer("Server1");
			c.setPort("2022");
	
			@SuppressWarnings("unchecked")
			String ret = resource.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(String.class, Publish.toJson(c));
					
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void deleteData() throws UnknownHostException, MongoException
	{
		DBInterface di = new DBInterface();
		
		di.deleteData();
	}
	public static void main(String[] args) throws UnknownHostException, MongoException {
		ReachOutClient bc = new ReachOutClient("http://localhost:8080/ReachOut/rest/community");

				
		//Charity
		
//		bc.demoGetParticularCharity();
		//bc.deleteData();
//		bc.demoCreateCharity();
//		bc.demolistCharities();
//		bc.demoGetCharityByName();
//		bc.donateCharity1();	
		
		
	//Discussion thread
	  // bc.demolistDiscussion();
	  // bc.demoCreateTopic();
      // bc.demoCreateComment(); 
		//create topic named "IPL" to test
	 // bc.demoGetTopicByName();
		//create topic named "IPL" to test
	  // bc.demoGetTopicDetails();
		
	    //Subscription
//		bc.demoSubscribe();
//		bc.demoServices();
	   
	   //Login
	   //bc.demoLogin();
	   //bc.demoPublish();
	}
}
